package com.aliyun.oss.controller;

import com.aliyun.oss.pojo.DTO.ShowUserInfoDTO;
import com.aliyun.oss.pojo.Entity.User;
import com.aliyun.oss.pojo.Vo.ResultVO;
import com.aliyun.oss.pojo.Vo.UserVO;
import com.aliyun.oss.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/getAllUsers")
    public ResultVO getAllUsers(){
        List<User> userList= userService.getAllUsers();
        return ResultVO.success(userList);
    }
    @GetMapping("/queryUserLikeByName")
    public ResultVO queryUserLikeByName(@RequestParam("name") String name){
        List<User> userList=userService.queryUserLikeByName(name);
        return ResultVO.success(userList);
    }
    @PatchMapping("/updateUserById")
    public ResultVO updateUserById(@RequestAttribute("id") Integer operatorId,@RequestBody ShowUserInfoDTO showUserInfoDTO){
        userService.updateUserById(operatorId,showUserInfoDTO);
        return ResultVO.success();
    }
    @PatchMapping("/updateUserStatusById")
    public ResultVO updateUserStatusById(@RequestAttribute("id") Integer operatorId,@RequestBody Map<String,Object> map){
        Integer id = (Integer) map.get("id");
        Integer status = (Integer) map.get("status");
        userService.updateUserStatusById(operatorId,id,status);
        return ResultVO.success();

    }

    @PostMapping("/addUser")
    public ResultVO addUser(@RequestAttribute("id") Integer operatorId,@RequestBody ShowUserInfoDTO showUserInfoDTO){
        userService.addUser(operatorId,showUserInfoDTO);
        return ResultVO.success();
    }
//    根据id查询所有消息
    @GetMapping("/getUserById")
    public ResultVO getUserById(@RequestAttribute("id") Integer operateId) {
        User user = userService.getById(operateId);
        UserVO userVO = null;
        if (user != null) {
            userVO = new UserVO(user.getId(), user.getPassword());
        }
        return ResultVO.success(userVO);
    }
//    根据id更新密码
    @PatchMapping("/updateUserPasswordById")

    public ResultVO updateUserPasswordById(@RequestAttribute("id") Integer operatorId, @RequestBody Map<String, Object> map) {
        String password = (String) map.get("password");
        
        userService.updateUserPasswordById(operatorId, password);
        return ResultVO.success();
    }
}
